import time
import allure
from config.setting import host_port
from pages.basePage2 import BasePage

#页面元素定位类
class HomePage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        time.sleep(2)
        self.driver.get(f"http://{host_port}/SharedParkingPlace/lessor/index.html")

    #定位 表单
    def get_frame_element(self):
        return self.driver.find_element_by_id('testIframe')

    #定位 添加车位按钮
    def get_addparking_element(self):
        return self.driver.find_element_by_xpath('//div[@id="add"]/a')

    #定位 输入车位号输入框
    def get_parkingid_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-body"]/div[1]/div[3]/input')

    #定位 大小id输入框
    def get_sizeid_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-body"]/div[2]/div[3]/input')

    #定位 位置id输入框
    def get_locationid_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-body"]/div[3]/div[3]/input')

    #定位 详细地址输入框
    def get_detailAddress_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-body"]/div[4]/div[3]/input')

    #定位 增加按钮
    def get_addbtn_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-footer"]/button[1]')

    #定位 删除按钮
    def get_delete_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[1]/div[2]/div/div/div[2]/p[4]/a[2]')

    #定位 修改按钮
    def get_alter_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[1]/div[2]/div/div/div[2]/p[4]/a[1]')

    #定位  弹窗-修改按钮
    def get_addalter_element(self):
        return self.driver.find_element_by_xpath('//div[@id="parkinginfo"]//div[@class="modal-footer"]/button[1]')

    #定位 弹窗断言
    def get_text_element(self):
        return self.driver.find_element_by_id('noticemessage')

    #定位 弹窗关闭按钮
    def get_closePop_element(self):
        return self.driver.find_element_by_xpath('//div[@id="notice"]//button[1]')

#操作层：点击、输入、选择
class HomePageHandle(HomePage):
    def __init__(self):
        HomePage.__init__(self)

    @allure.step('切换表单')
    def switch_frame(self):
        frame_element = self.get_frame_element()
        self.driver.switch_to.frame(frame_element)

    @allure.step('点击添加车位')
    def click_addparking(self):
        self.get_addparking_element().click()

    @allure.step('输入车位号')
    def send_parkingid(self, parkingid):
        self.get_parkingid_element().send_keys(parkingid)

    @allure.step('输入车位大小id')
    def send_sizeid(self, sizeid):
        size = self.get_sizeid_element()
        size.clear()
        size.send_keys(sizeid)

    @allure.step('输入位置id')
    def send_locationid(self, locationid):
        self.get_locationid_element().send_keys(locationid)

    @allure.step('输入详细地址')
    def send_detailAddress(self, detailedAddress):
        self.get_detailAddress_element().send_keys(detailedAddress)

    @allure.step('点击增加按钮')
    def click_add_buttton(self):
        self.get_addbtn_element().click()

    @allure.step('点击删除按钮')
    def click_deleteparking(self):
        self.get_delete_element().click()

    @allure.step('点击修改按钮')
    def click_alterbtn(self):
        self.get_alter_element().click()

    @allure.step('输入修改内容')
    def input_alter(self, content):
        input = self.get_detailAddress_element()
        input.clear()
        input.send_keys(content)

    @allure.step('点击修改按钮，提交修改')
    def click_addalter(self):
        self.get_addalter_element().click()

    @allure.step('获取弹窗信息')
    def get_text_box(self):
        return self.get_text_element().text

    @allure.step('点击弹窗关闭按钮')
    def click_closePop(self):
        self.get_closePop_element().click()

#业务层
class HomePageBusiness(HomePageHandle):
    def __init__(self):
        HomePageHandle.__init__(self)

    #添加车位
    def add_parking(self, parkingid, sizeid, locationid, detailedAddress):
        """
        :param parkingid: 车位号
        :param sizeid: 车位大小id
        :param locationid: 位置id
        :param detailedAddress: 详细地址
        :return:  弹窗文本信息
        """
        #切换表单
        self.switch_frame()
        #点击添加车位
        self.click_addparking()
        time.sleep(1)
        #输入车位号
        self.send_parkingid(parkingid)
        #输入大小id
        self.send_sizeid(sizeid)
        #输入位置id
        self.send_locationid(locationid)
        #输入详细地址
        self.send_detailAddress(detailedAddress)
        #点击增加按钮
        time.sleep(0.5)
        self.click_add_buttton()
        #获取的文本信息
        time.sleep(0.5)
        pop_text = self.get_text_box()
        #关闭弹窗
        self.click_closePop()
        return pop_text

    #删除车位
    def del_parking(self):
        # 切换表单
        self.switch_frame()
        #点击 删除车位
        self.click_deleteparking()
        #获取的文本信息
        time.sleep(0.5)
        pop_text = self.get_text_box()
        #关闭弹窗
        self.click_closePop()
        return pop_text

    #修改车位信息
    def alter_parking(self, content):
        # 切换表单
        self.switch_frame()
        #点击 修改按钮
        self.click_alterbtn()
        #输入 修改的内容
        self.input_alter(content)
        #点击 弹窗-修改按钮
        self.click_addalter()
        #获取弹窗断言信息
        time.sleep(0.5)
        pop_text = self.get_text_box()
        time.sleep(1)
        #关闭弹窗
        self.click_closePop()
        return pop_text



if __name__ == '__main__':
    h1 = HomePageBusiness()
    a = h1.add_parking('53', '1', '4006', '我也不知道详细地址，这是用来测试的')
    print(a)

    d = h1.del_parking()
    print(d)

    al = h1.alter_parking('修改车位信息专用测试语----玛卡巴卡-----')
    print(al)